﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using ApplicationGenerator.Forms.WebControls;

namespace GUserInterface
{
    public partial class ControlsToolbox : GToolbox, IToolboxService
    {

        #region Variables

        private ListBox ToolsListBox = new ListBox();
        private ToolboxItemCollection toolboxItems = new ToolboxItemCollection();
        private int selectedIndex = 0;
        private Panel EnabledToolboxPanel = new Panel();
        private Panel NotEnabledToolboxPanel = new Panel();
        private Label NotEnabledToolboxLabel = new Label();
     
        private Type[] ToolTypes = new Type[] 
        {
            /*typeof(System.Windows.Forms.ComboBox),
		    typeof(System.Windows.Forms.Label), 
            typeof(System.Windows.Forms.Button), 
            typeof(System.Windows.Forms.TextBox), 
            typeof(System.Windows.Forms.CheckBox), 
            typeof(System.Windows.Forms.ListBox),
            typeof(System.Windows.Forms.LinkLabel),
            typeof(System.Windows.Forms.ListView),
            typeof(WorkflowLibrary.DisplayPageActivity),*/
            typeof(CheckBoxControl),
            typeof(DropDownListControl),
            typeof(LabelControl),
            typeof(TextBoxControl),
            typeof(ButtonControl),
            //typeof(BusinessModule.Forms.DropDownListControl),
            typeof(DisplayListControl),
            //typeof(Lists.Forms.RelationWebControl),
            typeof(SetListControl),
            typeof(SetRelationControl)

		};

        #endregion

        #region Construction

        public ControlsToolbox()
        {
            InitializeComponent();
            InitializeToolbox();
            InitializeComponent_Custom();
        }

        private void InitializeComponent_Custom()
        {
            NotEnabledToolboxPanel.Visible = true;
            EnabledToolboxPanel.Visible = false;
            NotEnabledToolboxLabel.Text = "Toolbox not enabled at the moment";
            NotEnabledToolboxPanel.Controls.Add(NotEnabledToolboxLabel);
            this.Controls.Add(NotEnabledToolboxLabel);
            this.Controls.Add(EnabledToolboxPanel);
            NotEnabledToolboxPanel.Width = this.Width;
            NotEnabledToolboxLabel.Width = this.Width;
            EnabledToolboxPanel.Dock = DockStyle.Fill;
            
        }

        public void InitializeToolbox()
        {
            TabText = "Controls";
            FillToolboxItems();
            FillToolbox();
            AddEventHandlers();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Fills toolbox with available Toolbox Types
        /// </summary>
        private void FillToolboxItems()
        {
            toolboxItems.Clear();

            for (int i = 0; i < ToolTypes.Length; i++)
            {
                ToolboxItem toolboxItem = new ToolboxItem();

                toolboxItem.Type = ToolTypes[i];
                toolboxItem.Name = ToolTypes[i].Name;
                toolboxItems.Add(toolboxItem);
            }
        }

        /// <summary>
        /// adds event handlers 
        /// </summary>
        private void AddEventHandlers()
        {
            ToolsListBox.MouseDown += new System.Windows.Forms.MouseEventHandler(this.list_MouseDown);
            ToolsListBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.list_DrawItem);
        }

        /// <summary>
        /// called when listbox item is drawn.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void list_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            try
            {
                ListBox lbSender = sender as ListBox;
                if (lbSender == null)
                    return;

                // If this tool is the currently selected tool, draw it with a highlight.
                if (selectedIndex == e.Index)
                {
                    e.Graphics.FillRectangle(Brushes.LightSlateGray, e.Bounds);
                }

                System.Drawing.Design.ToolboxItem tbi = lbSender.Items[e.Index] as System.Drawing.Design.ToolboxItem;
                Rectangle BitmapBounds = new Rectangle(e.Bounds.Location.X, e.Bounds.Location.Y + e.Bounds.Height / 2 - tbi.Bitmap.Height / 2, tbi.Bitmap.Width, tbi.Bitmap.Height);
                Rectangle StringBounds = new Rectangle(e.Bounds.Location.X + BitmapBounds.Width + 5, e.Bounds.Location.Y, e.Bounds.Width - BitmapBounds.Width, e.Bounds.Height);

                StringFormat format = new StringFormat();

                format.LineAlignment = StringAlignment.Center;
                format.Alignment = StringAlignment.Near;
                e.Graphics.DrawImage(tbi.Bitmap, BitmapBounds);
                e.Graphics.DrawString(tbi.DisplayName, new Font("Tahoma", 11, FontStyle.Regular, GraphicsUnit.World), Brushes.Black, StringBounds, format);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void list_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            try
            {
                ListBox lbSender = sender as ListBox;
                Rectangle lastSelectedBounds = lbSender.GetItemRectangle(0);
                try
                {
                    lastSelectedBounds = lbSender.GetItemRectangle(selectedIndex);
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }

                selectedIndex = lbSender.IndexFromPoint(e.X, e.Y); // change our selection
                lbSender.SelectedIndex = selectedIndex;
                lbSender.Invalidate(lastSelectedBounds); // clear highlight from last selection
                lbSender.Invalidate(lbSender.GetItemRectangle(selectedIndex)); // highlight new one

                if (selectedIndex != ListBox.NoMatches)
                {
                    if (e.Clicks < 2)
                    {
                        System.Drawing.Design.ToolboxItem tbi = lbSender.Items[selectedIndex] as System.Drawing.Design.ToolboxItem;
                        IToolboxService tbs = this;

                        string type = tbi.TypeName;
                        try
                        {
                            lbSender.DoDragDrop(type, DragDropEffects.Copy);
                        }
                        catch (Exception)
                        {
                            //error case
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        public void FillToolbox()
        {
            CreateControls();
            ConfigureControls();
            UpdateToolboxItems();
        }

        private void CreateControls()
        {
            Controls.Clear();
            ToolsListBox = new ListBox();
        }


        private void ConfigureControls()
        {
            EnabledToolboxPanel.SuspendLayout();

            ToolsListBox.ScrollAlwaysVisible = true;
            ToolsListBox.BackColor = System.Drawing.SystemColors.ControlLight;
            ToolsListBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            ToolsListBox.ItemHeight = 18;
            ToolsListBox.Location = new System.Drawing.Point(0, 40);
            ToolsListBox.Name = "ToolsListBox";
            ToolsListBox.Size = new System.Drawing.Size(this.Width, this.Height);

            UpdateToolboxItems();
            EnabledToolboxPanel.Controls.Add(ToolsListBox);

            EnabledToolboxPanel.ResumeLayout();
            //this.ToolsListBox = listBox;
            this.SizeChanged += new EventHandler(Toolbox_SizeChanged);
        }

        /// <summary>
        /// updates images and look of toolbox items
        /// </summary>
        private void UpdateToolboxItems()
        {
            ToolsListBox.Items.Clear();

            foreach (ToolboxItem toolboxItem in toolboxItems)
            {
                Type type = toolboxItem.Type;
                System.Drawing.Design.ToolboxItem tbi = new System.Drawing.Design.ToolboxItem(type);
                System.Drawing.ToolboxBitmapAttribute tba = TypeDescriptor.GetAttributes(type)[typeof(System.Drawing.ToolboxBitmapAttribute)] as System.Drawing.ToolboxBitmapAttribute;

                if (tba != null)
                {
                    tbi.Bitmap = (System.Drawing.Bitmap)tba.GetImage(type);
                }

                this.ToolsListBox.Items.Add(tbi);
            }

        }

        /// <summary>
        /// handler - on size changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Toolbox_SizeChanged(object sender, EventArgs e)
        {
            this.ToolsListBox.Size = new System.Drawing.Size(this.Width, this.Height - 40);
        }

        /// <summary>
        /// shows proper toolbox view
        /// </summary>
        /// <param name="flag"></param>
        public void ShowToolbox(bool flag)
        {
            if (true == flag)
            {
                NotEnabledToolboxLabel.Visible = false;
                EnabledToolboxPanel.Visible = true;
            }
            else
            {
                NotEnabledToolboxLabel.Visible = true;
                EnabledToolboxPanel.Visible = false;
            }

        }

        #endregion

        #region IToolboxService Members

        public void AddCreator(ToolboxItemCreatorCallback creator, string format, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public void AddCreator(ToolboxItemCreatorCallback creator, string format)
        {
            throw new NotImplementedException();
        }

        public void AddLinkedToolboxItem(ToolboxItem toolboxItem, string category, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public void AddLinkedToolboxItem(ToolboxItem toolboxItem, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public void AddToolboxItem(ToolboxItem toolboxItem, string category)
        {
            throw new NotImplementedException();
        }

        public void AddToolboxItem(ToolboxItem toolboxItem)
        {
            throw new NotImplementedException();
        }

        public CategoryNameCollection CategoryNames
        {
            get { throw new NotImplementedException(); }
        }

        public ToolboxItem DeserializeToolboxItem(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public ToolboxItem DeserializeToolboxItem(object serializedObject)
        {
            throw new NotImplementedException();
        }

        public ToolboxItem GetSelectedToolboxItem(System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public ToolboxItem GetSelectedToolboxItem()
        {
            throw new NotImplementedException();
        }

        public ToolboxItemCollection GetToolboxItems(string category, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public ToolboxItemCollection GetToolboxItems(string category)
        {
            throw new NotImplementedException();
        }

        public ToolboxItemCollection GetToolboxItems(System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public ToolboxItemCollection GetToolboxItems()
        {
            throw new NotImplementedException();
        }

        public bool IsSupported(object serializedObject, System.Collections.ICollection filterAttributes)
        {
            throw new NotImplementedException();
        }

        public bool IsSupported(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public bool IsToolboxItem(object serializedObject, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public bool IsToolboxItem(object serializedObject)
        {
            throw new NotImplementedException();
        }

        public void RemoveCreator(string format, System.ComponentModel.Design.IDesignerHost host)
        {
            throw new NotImplementedException();
        }

        public void RemoveCreator(string format)
        {
            throw new NotImplementedException();
        }

        public void RemoveToolboxItem(ToolboxItem toolboxItem, string category)
        {
            throw new NotImplementedException();
        }

        public void RemoveToolboxItem(ToolboxItem toolboxItem)
        {
            throw new NotImplementedException();
        }

        public string SelectedCategory
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void SelectedToolboxItemUsed()
        {
            ListBox list = this.ToolsListBox;

            list.Invalidate(list.GetItemRectangle(selectedIndex));
            selectedIndex = 0;
            list.SelectedIndex = 0;
            list.Invalidate(list.GetItemRectangle(selectedIndex));
        }

        public object SerializeToolboxItem(ToolboxItem toolboxItem)
        {
            throw new NotImplementedException();
        }

        public bool SetCursor()
        {
            throw new NotImplementedException();
        }

        public void SetSelectedToolboxItem(ToolboxItem toolboxItem)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
